package com.zdb.n1.controller;

import com.zdb.n1.entity.HealthData;


import com.zdb.n1.payload.response.MessageResponse;
import com.zdb.n1.service.HealthDataService;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/health-data")
public class HealthDataController {

    private static final Logger logger = LoggerFactory.getLogger(HealthDataController.class);

    @Autowired
    private HealthDataService healthDataService;

    @PostMapping
    public ResponseEntity<HealthData> createHealthData(@Valid @RequestBody HealthData healthData) {
            HealthData savedData = healthDataService.saveHealthData(healthData);
            return ResponseEntity.ok(savedData);

    }

    @GetMapping("/{id}")
    public ResponseEntity<HealthData> getHealthDataById(@PathVariable Long id) {
        return ResponseEntity.ok(healthDataService.getHealthDataById(id));
    }

    @GetMapping
    public ResponseEntity<List<HealthData>> getAllHealthData() {
        return ResponseEntity.ok(healthDataService.getAllHealthDataForCurrentUser());
    }

    @GetMapping("/paged")
    public ResponseEntity<Page<HealthData>> getPagedHealthData(Pageable pageable) {
        return ResponseEntity.ok(healthDataService.getHealthDataForCurrentUser(pageable));
    }

    @GetMapping("/range")
    public ResponseEntity<List<HealthData>> getHealthDataByDateRange(
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startDate,
            @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endDate) {

        
        try {
            // 直接使用传入的日期，不进行时区转换
            List<HealthData> dataList = healthDataService.getHealthDataByDateRange(startDate, endDate);
            return ResponseEntity.ok(dataList);
        } catch (Exception e) {
            throw e;
        }
    }

    @GetMapping("/latest")
    public ResponseEntity<?> getLatestHealthData() {
        HealthData latestData = healthDataService.getLatestHealthData();
        if (latestData == null) {
            return ResponseEntity.ok(new HashMap<>());
        }
        
        Map<String, Object> response = new HashMap<>();
        response.put("healthData", latestData);
        response.put("bmiCategory", healthDataService.getBmiCategory(latestData.getBmi()));
        
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<MessageResponse> deleteHealthData(@PathVariable Long id) {
        return ResponseEntity.ok(healthDataService.deleteHealthData(id));
    }
} 